package com.golden.procedure.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.golden.procedure.domain.QueryRequest;
import com.golden.procedure.domain.SingleRequest;
import com.golden.procedure.domain.SiteDO;
import com.golden.procedure.query.SiteQueryDO;
import com.golden.procedure.result.PageResponse;
import com.golden.procedure.result.ResultDO;
import com.golden.procedure.result.SingleResponse;
import com.golden.procedure.service.ISiteService;
import com.golden.procedure.util.EncryptedUtil;
import com.golden.procedure.util.HttpStatus;
import com.google.gson.Gson;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value = "/api/site")
public class SiteController extends BaseController {

	@Autowired
	ISiteService siteService;

	public static String encodeObject(SingleResponse<SiteDO> response, String encryptType) {
		Gson gson = new Gson();
		String jsonData = gson.toJson(response);
		try {
			return EncryptedUtil.encrypt(jsonData, encryptType);
		} catch (Exception e) {
			e.printStackTrace();
			return SYS_ERROR;
		}
	}

	public static String encodePage(PageResponse<SiteDO> response, String encryptType) {
		Gson gson = new Gson();
		String jsonData = gson.toJson(response);
		try {
			return EncryptedUtil.encrypt(jsonData, encryptType);
		} catch (Exception e) {
			e.printStackTrace();
			return SYS_ERROR;
		}
	}

	@ApiOperation(value = "获取上门组列表,同时获得最新排控日期", notes = "")
	@ApiImplicitParams({ @ApiImplicitParam(name = "province", value = "所在省code", required = true, dataType = "String"),
			@ApiImplicitParam(name = "city", value = "所在市code", required = true, dataType = "String") })
	@RequestMapping(value = { "/query.json" }, method = RequestMethod.POST)
	public String querySite(@RequestBody QueryRequest<SiteQueryDO> request) {

		SiteQueryDO query = request.getData();
		query.setCategory(22); // 上门组category为22

		PageResponse<SiteDO> response = new PageResponse<SiteDO>();
		ResultDO resultDO = siteService.query(query);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
			response.setData((List<SiteDO>) resultDO.getModel(ResultDO.FIRST_MODEL_KEY));
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(0);
			response.setMsg(resultDO.getErrorMsg());
		}
		return encodePage(response, PLANINTEXT);
	}

	@ApiOperation(value = "获取上门组详情", notes = "根据id来获取上门组详细信息")
	@ApiImplicitParam(name = "id", value = "1", required = true, dataType = "Integer")
	@RequestMapping(value = "/get.json", method = RequestMethod.POST)
	public String getSite(@RequestBody SingleRequest<SiteQueryDO> request) {

		SiteQueryDO query = request.getData();
		SingleResponse<SiteDO> response = new SingleResponse<SiteDO>();
		ResultDO resultDO = siteService.get(query.getId());
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
			response.setData((SiteDO) resultDO.getModel(ResultDO.FIRST_MODEL_KEY));
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(0);
			response.setMsg(resultDO.getErrorMsg());
		}
		return encodeObject(response, PLANINTEXT);
	}

}
